package org.pkgit.app.bqs.retrofitdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    TextView tv;
    int DEFAULT_TIME_OUT = 1500;
    int DEFAULT_READ_TIME_OUT = 1500;
    private static final String TAG = "Frank";

    private final CompositeDisposable disposables = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);

        //RXJAVA与Retrofit混合使用
        hunheUser();

    }

    private void hunheUser() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
                .build();

        Weather weather = retrofit.create(Weather.class);


        ApiClient.retrofit().create(Weather.class).getNews("北京","1790c3b1bbaf32af326e12dbdc818c9c")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<News>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e(TAG, "onSubscribe: d="+d.toString() );
                    }

                    @Override
                    public void onNext(final News news) {
                        Log.e("Frank", "onNext: news="+news.getResult().getFuture().toString());
                        tv.post(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText(news.getResult().getFuture().toString());
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: e="+e.toString() );
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: " );
                    }
                });



    }

    public void setRetrofit(){
       Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
                .build();
//
        Weather weather = retrofit.create(Weather.class);



//        Call<News> call = weather.getNews("北京", "1790c3b1bbaf32af326e12dbdc818c9c");
//        call.enqueue(new Callback<News>() {
//            @Override
//            public void onResponse(Call<News> call, Response<News> response) {
//                News body = response.body();
//                final News.ResultBean result = body.getResult();
//
//                tv.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        tv.setText(result.getFuture().toString());
//                    }
//                });
//
//                Log.e("Frank", "onResponse: bode=" + response.body().getResult().getFuture().toString());
//            }
//
//            @Override
//            public void onFailure(Call<News> call, Throwable t) {
//
//            }
//        });
//
//
//        //创建被观察者 observable &产生事件
//        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
//            @Override
//            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
//
//            }
//        });
//
//
//        //创建观察者 （Observer ）并 定义响应事件的行为
//        Observer<Integer> observer = new Observer<Integer>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//                Log.e("Frank", "onSubscribe: 开始连接");
//            }
//
//            @Override
//            public void onNext(Integer integer) {
//                Log.e("Frank", "onSubscribe: 处理事件" + integer);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                Log.e("Frank", "onSubscribe: 处理事件" + e);
//            }
//
//            @Override
//            public void onComplete() {
//                Log.e("Frank", "onSubscribe: 事件完成.不在接收任何事件");
//            }
//        };
//
//        //通过订阅（Subscribe）连接观察者和被观察者
//        observable.subscribe(observer);
//
//        //基于事件流链式方式
//        Observable.create(new ObservableOnSubscribe<Integer>() {
//            @Override
//            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
//
//            }
//        }).subscribe(new Observer<Integer>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(Integer integer) {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });
    }
}
